package com.huawei.admins.platform.gateway;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.security.reactive.ReactiveManagementWebSecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.ldap.LdapAutoConfiguration;
import org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.session.SessionAutoConfiguration;
import org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import java.util.Arrays;

@SpringBootApplication(exclude = {
        ReactiveSecurityAutoConfiguration.class,
        ReactiveManagementWebSecurityAutoConfiguration.class,
        SessionAutoConfiguration.class,
        LdapAutoConfiguration.class,
        HttpHandlerAutoConfiguration.class
})
@ComponentScan(basePackages = {
        "com.huawei.admins",
        "com.sharework.micro",
})
public class ApplicationServer {

    private static final Logger logger = LoggerFactory.getLogger(ApplicationServer.class);

    public static void main(String[] args) {

        SpringApplication application = new SpringApplicationBuilder(ApplicationServer.class).build();
        application.addInitializers();
        application.addListeners(applicationListener());
        application.run(args);
    }

    private static ApplicationListener<ApplicationReadyEvent> applicationListener() {
        return event -> {
            ConfigurableApplicationContext ctx = event.getApplicationContext();
            logger.info("=========================== ApplicationListener =============================");
            logger.debug("There are all of the gateway beans provided by Spring Boot Application Context:");
            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                Object bean = ctx.getBean(beanName);
                logger.debug("name: {}, class: {}", beanName, bean.getClass().getName());
            }
            logger.debug("============================================================================");
        };
    }
}
